<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Session;
use think\facade\Db;

class GmTgczlogs extends Model
{
    use SoftDelete;
     protected $deleteTime = false;
    // 获取列表
   
    public static function getList()
    {
        // 初始化 WHERE 子句
        $where = [];

        // 获取每页显示的记录数和当前页数
        $limit = input('get.limit');
        $page = input('get.page', 1); // 默认为第一页

        // 如果不是超级管理员，添加限制条件
        if (Session::get('admin.id') != '1') {
            $where[] = ['c.id', '=', Session::get('admin.id')];
            $where[] = ['a.status', '=', '1'];
        }else{
            $where[] = ['a.status', '=', '1'];
        }

        // 根据状态筛选
        $status = input('status');
        if (!empty($status)) {
            $statusMap = [
                '000' => 0,
                '111' => 1,
            ];
            if (isset($statusMap[$status])) {
                $where[] = ['a.status', '=', $statusMap[$status]];
            }
        }

        // 根据充值类型筛选 
        $cztype = input('cztype');
        if (!empty($cztype)) {
            $where[] = ['a.cztype', '=', $cztype];
        }

        // 推广人账号
        $nickename = input('nickename');
        if (!empty($nickename)) {
            $where[] = ['c.nickname', 'like', "%$nickename%"];
        }

        // 根据订单号筛选
        $ddcode = input('ddcode');
        if (!empty($ddcode)) {
            $where[] = ['a.ddcode', '=', $ddcode];
        }

        // 根据玩家名称模糊查询
        $actorname = input('actorname');
        if (!empty($actorname)) {
            $where[] = ['a.actorname', 'like', "%$actorname%"];
        }

        //按充值时间查找
        $start = input("get.cztime-start");
        $end = input("get.cztime-end");
        if ($start && $end) {
            $where[]=["a.cztime","between",[$start,date("Y-m-d",strtotime("$end +1 day"))]];
        }

        // 构建 SQL 查询语句
        $sql = Db::table('gm_tgczlogs')
            ->alias('a')
            ->leftjoin('gm_tglogs b', 'a.actorname = b.baccount')
            ->leftjoin('admin_admin c', 'b.acode = c.rlink')
            ->field('c.nickname AS nickename, a.serverid AS serverid, a.actorid AS actorid, a.actorname AS actorname, a.actoraccount AS actoraccount, a.cztime AS cztime, a.ddcode AS ddcode, a.jine AS jine, a.cztype AS cztype, a.status AS status')
            ->where($where)
            ->order('a.cztime', 'desc')
            ->paginate($limit);

        return ['code' => 0, 'data' => $sql->items(), 'extend' => ['count' => $sql->total(), 'limit' => $limit]];
    }

    
}
